<script src="js/common2.js"></script>

<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'west',border:true,split:true," title="分类管理" style="width:150px; padding:5px;">
       	<table id="treegrid_dg" class="easyui-treegrid" style="width:600px;height:400px"   
        data-options="fit:true">   
    <thead>   
        <tr >  
        	<th  data-options="field:'protypename'">分类名称</th>
            <!--<th data-options="field:'permissionValue'">权限资源对象</th>-->   
            <!--<th data-options="field:'updateTime'">权限最近一次修改时间</th>-->
            <!-- <th data-options="field:'setLock',align:'center',width:100,formatter:operation">操作</th>-->
        </tr>   
    </thead>   
</table> 
    </div>
    <div data-options="region:'center',border:false">
    	<!-- Begin of toolbar -->
        <div id="wu-toolbar">
            <div class="wu-toolbar-button">
                <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openAdd()" plain="true">进货开单</a>
               <!-- <a href="#" class="easyui-linkbutton" iconCls="icon-user" onclick="openAdd()" plain="true">申请开单</a>-->
                <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="selectUser()" plain="true">刷新</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-help" onclick="openEdit()" plain="true">帮助</a>
                <!--<a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="openEdit()" plain="true">修改</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="remove()" plain="true">删除</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="cancel()" plain="true">取消</a>
                
                <a href="#" class="easyui-linkbutton" iconCls="icon-print" onclick="openAdd()" plain="true">打印</a>
                
                <a href="#" class="easyui-linkbutton" iconCls="icon-undo" onclick="remove()" plain="true">撤销</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-redo" onclick="cancel()" plain="true">重做</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-sum" onclick="reload()" plain="true">总计</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-back" onclick="reload()" plain="true">返回</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-tip" onclick="reload()" plain="true">提示</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-save" onclick="reload()" plain="true">保存</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-cut" onclick="reload()" plain="true">剪切</a>-->
            </div>
            <form>
            <div class="wu-toolbar-search">
            	  <label>名称：</label><input id="uname" class="wu-text" style="width:100px">
                <label>进货开单时间：</label>
                <input id="createTime11" class="easyui-datebox" style="width:100px">
                <label>~~
                </label><input id="endTime11"  class="easyui-datebox" style="width:100px">
                 <label>库存量：</label>
                <input id="startnumber"  class="wu-text" style="width:100px">
                ~~<input id="endnumber"   class="wu-text" style="width:100px">
              
                <!--<label>用户组：</label> 
                <select class="easyui-combobox" panelHeight="auto" style="width:100px">
                    <option value="0">选择用户组</option>
                    <option value="1">黄钻</option>
                    <option value="2">红钻</option>
                    <option value="3">蓝钻</option>
                </select>-->
              
                <a href="#" class="easyui-linkbutton" onclick="selectUser()" iconCls="icon-search">开始检索</a>
                
            </div>
            </form>
        </div>
        <!-- End of toolbar -->
        <table id="wu-datagrid" toolbar="#wu-toolbar"></table>
    </div>
</div>

<div id="dlgdiv" class="easyui-dialog"
     style="width: 650px; height: 550px; padding: 10px 20px" closed="true"
     buttons="#dlgdiv-buttons">
        <div id="divlarge"></div>
</div>

<div id="jhdivid" class="easyui-window"  title="开单" data-options="closed:true" style="width:400px; height: 400px; padding:10px;">
 <label>请选择供应商:</label><br />
 <input type="text" id="sectionId17"  panelHeight=auto style="width:180px;" class="easyui-combotree"  />
 <br /><!-- <label>请选择开单日期:</label><br />
 <div id="rili" class="easyui-calendar" style="width:180px;height:180px;"></div>	-->
 <input class="easyui-linkbutton"  type="button" onclick="xuanzekaidan()"/>
</div>


<!-- Begin of easyui-dialog -->
<!--修改框-->
<div id="wu-dialog" class="easyui-dialog"  title="修改信息"   data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
	<form id="wu-form" method="post">
        <table>
        	<input type="text" hidden="hidden" name="proid" class="wu-text" />
            <tr>
                <td width="60" align="right">产品名称:</td>
                <td><input type="text" name="proname" class="wu-text" /></td>
            </tr>
            <tr>
                <td align="right">产品类型:</td>
                <td><input type="text" id="sectionId13" name="protype"  panelHeight=auto style="width:268px;" class="easyui-combobox"  /></td>
            </tr>
              <tr>
                <td align="right">产品供应商:</td>
                <td><input type="text" id="sectionId15" name="prosupplier"  panelHeight=auto style="width:268px;" class="easyui-combobox"  /></td>
            </tr>
            <tr>
                <td align="right">产品进价:</td>
                <td><input type="text" name="projinjia" class="wu-text" /></td>
            </tr>
              <tr>
                <td align="right">产品售价:</td>
                <td><input type="text" name="proshoujia" class="wu-text" /></td>
            </tr>
              <tr>
                <td align="right">产品数量:</td>
                <td><input type="text" name="pronumber" class="wu-text" /></td>
            </tr>
              <tr>
                <td align="right">警报数量:</td>
                <td><input type="text" name="alarmnumber" class="wu-text" /></td>
            </tr>
            <tr>
                <td align="right">商品单位:</td>
                <td><input type="" name="prodanwei" class="wu-text" /></td>
            </tr>
             
             <tr>
                <td align="right">商品图片:</td>
                <td><input type="text" name="proimg" class="wu-text" /></td>
            </tr>
              <tr>
                <td align="right">商品详情:</td>
                <td><input type="text" name="prodetails" class="wu-text" /></td>
            </tr>
          <!--  <tr>
                <td align="right">锁 定:</td>
                <td><select  class="easyui-combobox" name="isLockout" panelHeight=auto style="width:268px;">
						<option value=""></option>
						<option value="是">是</option>
						<option value="否">否</option>
					</select></td>
            </tr>-->
           <!-- <tr>
                <td valign="top" align="right">内 容:</td>
                <td><textarea name="content" rows="6" class="wu-textarea" style="width:260px"></textarea></td>
            </tr>-->
        </table>
    </form>
    <div style="text-align:center;padding:5px">
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="updateSaveUser()">保存</a>
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearUptateUserForm()">清空</a>
			</div>
</div>
<!--添加框-->
<div id="add-wu-dialog" class="easyui-dialog"  title="添加信息"   data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
	<form id="add-wu-form" method="post">
        <table>
        	<!--<input type="text" hidden="hidden" name="proid" class="wu-text" />-->
            <tr>
                <td width="60" align="right">产品名称:</td>
                <td><input type="text" id="sectionId16" name="proname"  panelHeight=auto style="width:268px;" class="easyui-combotree"  /></td>
            </tr>
            <tr>
                <td align="right">产品类型:</td>
                <td><input type="text" id="sectionId14" name="protype"  panelHeight=auto style="width:268px;" class="easyui-combobox"  /></td>
            </tr>
            <tr>
                <td align="right">产品进价:</td>
                <td><input type="text" name="projinjia" class="wu-text" /></td>
            </tr>
              <tr>
                <td align="right">产品售价:</td>
                <td><input type="text" name="proshoujia" class="wu-text" /></td>
            </tr>
              <tr>
                <td align="right">产品数量:</td>
                <td><input type="text" name="pronumber" class="wu-text" /></td>
            </tr>
              <tr>
                <td align="right">警报数量:</td>
                <td><input type="text" name="alarmnumber" class="wu-text" /></td>
            </tr>
             <tr>
                <td align="right">商品单位:</td>
                <td><input type="" name="prodanwei" class="wu-text" /></td>
            </tr>
           
             <tr>
                <td align="right">商品图片:</td>
                <td><input type="text" name="proimg" class="wu-text" /></td>
            </tr>
               <tr>
                <td align="right">商品详情:</td>
                <td><input type="text" name="prodetails" class="wu-text" /></td>
            </tr>
          <!--  <tr>
                <td align="right">锁 定:</td>
                <td><select  class="easyui-combobox" name="isLockout" panelHeight=auto style="width:268px;">
						<option value=""></option>
						<option value="是">是</option>
						<option value="否">否</option>
					</select></td>
            </tr>-->
           <!-- <tr>
                <td valign="top" align="right">内 容:</td>
                <td><textarea name="content" rows="6" class="wu-textarea" style="width:260px"></textarea></td>
            </tr>-->
        </table>
    </form>
    <div style="text-align:center;padding:5px">
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="addSaveUser()">保存</a>
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearAddUserForm()">清空</a>
			</div>
</div>
 
<div id="role_window" class="easyui-window" title="你正在对<span id='tname' style='color:black;'></span>进行权限设置" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:500px;height:500px;padding:16px;">
			<table>
				<tr>
					<td>
						<table name="center" id="AllRoles" class="easyui-datagrid" title="系统所有角色" data-options="rownumbers:true,singleSelect:true,method:'post',width:200,height:400">
							<thead>
								<tr>
									<!--<th field="ck" checkbox="true"></th>-->
									<th data-options="field:'rname',width:100">名称</th>
								</tr>
							</thead>
						</table>
					</td>
					<td>
						<input type="button" value=">>" onclick="jueseAdd()" />
						<br />
						<input type="button" value="<<" onclick="jueseRemove()" />
					</td>
					<td>
						<table name="right" id="partRoles" class="easyui-datagrid" title="当前用户的角色" data-options="rownumbers:true,singleSelect:true,method:'post',width:200,height:400">
							<thead>
								<th data-options="field:'id',width:100,hidden:true"></th>
								<th data-options="field:'rname',width:100">名称</th>
							</thead>
						</table>
					</td>
				</tr>
			</table>
		</div>
		<div class="easyui-window" id="uploadimg"   data-options="closed:true">
		<form id="fm" method="post" enctype="multipart/form-data">
           请选择文件： <input type="file" name="myfile" /><br/>
        <input type="button" value="提交" onclick="upload()"/>
       </form>
       </div>
<!-- End of easyui-dialog -->
<script type="text/javascript">
	
	 
	 //打开添加框
	  function xuanzekaidan(){
     $("#add-wu-form").form("clear");
       var t = $('#sectionId17').combotree('tree').tree('getSelected').id;	// get the tree object
   
	      $('#sectionId16').combotree({    
               url:globalData.server + 'product/selectDeptChildrenById?token='+globalData.getToken()+'&sid='+t,    
               //required: true
               });
               $("#jhdivid").window("close");
	$("#add-wu-dialog").window("open"); //打开窗口	
	}
	
	//打开修改图片框
	var updateImgProId
	function dakai(index){
		var data=$("#wu-datagrid").datagrid("getData")
     	var row = data.rows[index]; //获取验第index行对应的json数据 
        updateImgProId=row.proid
		$("#uploadimg").window("open")
	}
	
	
	  //上传图片
     function upload(){
     	alert(updateImgProId)
     	var formData = new FormData($('#fm')[0]);
       $.ajax({
           type:"post",
           url:globalData.server+"product/singleFileUpload2?token="+globalData.getToken()+"&proid="+updateImgProId,
           data:formData,
           dataType:"json",
           processData : false,
		   contentType : false,
           success:function(res){
             if(res.success){
                alert("上传成功！");
                $("#uploadimg").window("close")
                $("#wu-datagrid").datagrid("reload");
             }else{
               alert("上传失败！");
             }
           }
       });
     }
	
	    //刷新页面
	   function reload_s(){
		 $('#wu-datagrid').datagrid("reload")
		 }
	
	
   /*左侧树形表格*/	
	$(function(){
	search();		
	})
	function search(){
	$('#treegrid_dg').treegrid({    
    url:globalData.server+'productType/getProductTypeAll?token='+globalData.getToken(),
    idField:'protypename',    
    treeField:'protypename',
    onClickCell:function(field,row){
        $('#wu-datagrid').datagrid({
		url:globalData.server+"product/selectProductPage",
		queryParams: {
						token: globalData.getToken(),
						protype:row.protypeid,
					},})
        }
      }); }
	
	
	
	
	/**
	* Name 载入数据
	*/
	$(function(){
		   $('#sectionId13').combobox({    
              url:globalData.server + 'productType/getProductTypeAll?token='+globalData.getToken(),    
              valueField:'protypeid',    
              textField:'protypename'   
                });
		 $('#sectionId14').combobox({    
              url:globalData.server + 'productType/getProductTypeAll?token='+globalData.getToken(),    
              valueField:'protypeid',    
              textField:'protypename'   
            });
             $('#sectionId15').combobox({    
              url:globalData.server + 'product/selectSupplierAll?token='+globalData.getToken(),    
              valueField:'supplierid',    
              textField:'sname'   
            });
             
               $('#sectionId17').combotree({    
              url:globalData.server + 'product/queryDeptTreeLists?token='+globalData.getToken(),    
               //required: true
            });
		setTimeout(function(){
			selectUser();
		},1000);
	})
	
	
	/*查询所有用户信息*/
	function selectUser(){
	$('#wu-datagrid').datagrid({
		url:globalData.server+"product/selectProductPage",
		queryParams: {
						token: globalData.getToken(),
						proname:$("#uname").val(),
						createTime:$("#createTime11").datebox('getValue'),	
						endTime:$("#endTime11").datebox('getValue'),
						startpronumber:$("#startnumber").val(),
						endpronumber:$("#endnumber").val()
					},
				onLoadSuccess: function(data) {
					$("a[name='sc']").linkbutton(({
						text: "删除",
						iconCls: "icon-remove",
						//plain: true
					})), $("a[name='bj']").linkbutton(({
						text: "编辑",
						iconCls: "icon-edit",
						//plain: true
					})), $("a[name='mk']").linkbutton(({
						text: "角色设置",
						iconCls: "icon-user-group",
						//plain: true
					}))
					, $("a[name='imgnameupdate']").linkbutton(({
						text: "修改图片",
						iconCls: "icon-undo",
						iconAlign:"center"
						//plain: true
					}))
				
				}			
		,
		rownumbers:true,
		pageSize:4, 
		pageList:[4,6,8,10],
		pagination:true,
		multiSort:true,
		fitColumns:true,
		fit:true,
	columns:[[
			//{ checkbox:true},
			//{ field:'proid',title:'ID',width:80},
			{ field:'proname',title:'产品名称',width:80},
			{ field:'protypename',title:'产品类型',width:80,
			formatter:function(value,row,index){if(row.productType){return row.productType.protypename}}},
			{ field:'text',title:'产品供应商',width:80,
			formatter:function(value,row,index){if(row.supplier){return row.supplier.text}}},
			{ field:'projinjia',title:'产品进价',width:40},
			//{ field:'lastLoginTime',title:'登陆时间',width:100},
			{ field:'proshoujia',title:'产品售价',width:60},
			//{ field:'endLockTime',title:'被锁定时间',width:100},
			{ field:'pronumber',title:'产品数量',width:100},
			{ field:'alarmnumber',title:'警报数量',width:100},
		    { field:'prodanwei',title:'商品单位',width:100,sortable:true},
			{ field:'proimg',title:'商品图片',width:100,formatter:showimgs},
			//{ field:'prodetails',title:'商品详情',width:100,sortable:true},
			//{ field:'isLogin',title:'是否登陆',width:50},
			{ field:'caozuo',title:'修改操作',width:50,formatter:uupdate},
			{ field:'caozuo1',title:'删除操作',width:50,formatter:udelete},
			/*{ field:'caozuo2',title:'分配操作',width:50,formatter:ufenpei},*/
		]]
	});}
	
	function showimgs(value,row,index){
		if(value){ 
        return "<img  name='smallImg' onclick='myfunciton(this.src)'  src='images/"+value+"' alt='没有图片'  style=width:80px;height:50px;><a     name='imgnameupdate' class='easyui-linkbutton'  onclick='dakai("+index+")'   value='修改'/>"
    }else{
        return "<div style='height: 40px'><h2  align='center' style='padding-top: 0px' />😙你还没有上传</h2><div>";    
    }

	}
	
	function myfunciton(e) {
    var large='<img width="596px" height="453px" src=' +e+ '></img>';
    $("#divlarge").html(large).animate({
        height:'30%',
        width:'30%'
    },1);
    $("#dlgdiv").dialog("open").dialog("setTitle", "图片展示");
       }

	
	function uupdate(value,row,index){
	return "<a class='easyui-linkbutton' name='bj'  onclick='uupdates(" +index 
	+")'>修改信息</a>"        
	}
	function udelete(value,row,index){
	return "<a   class='easyui-linkbutton' name='sc'  onclick='udeletes(" +index 
	+")'>删除信息</a>"        
	}
/*	function ufenpei(value,row,index){
	return "<a  class='easyui-linkbutton' name='mk'   onclick='ufenpeis(" +index 
	+")'>分配模块</a>"        
	}*/
	
	/**
	 * 为用户分配角色
	 */
	var useridsss; //用户id
	var userNamesss;//用户名称
	function ufenpeis(index){
		var data = $("#wu-datagrid").datagrid("getData");
			var row = data.rows[index].uname //获取验第index行对应的json数据
			userNamesss = row
			var ids = data.rows[index].uid
			alert(ids)
			useridsss = ids	
			$("#tname").text(row);
			$("#AllRoles").datagrid({
				url: globalData.server + 'roles/getAll',
				queryParams: {
					token: globalData.getToken()
				}
			});
			$("#partRoles").datagrid({
				url: globalData.server + 'roles/getAllByUserId?uid='+ids,
				queryParams: {
					token: globalData.getToken()
				}
			});
	$("#role_window").window("open");		
	}
	/**
	* Name 删除记录
	*/
	function udeletes(index){
		$.messager.confirm('信息提示','确定要删除该记录？', function(result){
			if(result){
				var data = $('#wu-datagrid').datagrid('getData');
				var ids=data.rows[index].proid
				/*var items = $('#wu-datagrid').datagrid('getSelections');
				var ids = [];
				$(items).each(function(){
					ids.push(this.productid);	
				});*/
				//alert(ids);return;
				$.ajax({
					url:globalData.server+'product/del?token='+globalData.getToken()+"&proid="+ids,
					type: "post",
					dataType: 'json',
					success:function(res){
						if(res>0){
							$.messager.alert('信息提示','删除成功！','info');		
						}
						else
						{
							$.messager.alert('信息提示','删除失败！','info');		
						}
					}	
				});
			}	
		});
	}
	/**
	 * 回显修改框
	 */
	var updateUid;
function uupdates(index) {
			var data = $("#wu-datagrid").datagrid("getData");
			var row = data.rows[index]; //获取验第index行对应的json数据
			updateUid = row.proid
				/*$("#update_window").window({title:"您正在修改"+row.loginName+"的用户信息"});*/
			$("#tnames").text(row.proname);
			$("#wu-form").form("load", row);
			$("#wu-dialog").window("open"); //打开窗口
		}
//保存修改
function updateSaveUser(){
	$.messager.confirm('提示', '确认修改吗?', function(r) {
					if(r) {
						$.post(
							globalData.server + "product/update?token="+globalData.getToken(), 	
							$("#wu-form").serialize()
							,
							function(res) {
								if(res.success==false) {
									$.messager.alert('提示', res.msg);
									$("#updateRoles").window("close");
									$("#wu-datagrid").datagrid("reload");
								} else {
									$.messager.alert('提示', res.msg);
									$("#wu-dialog").window("close");
									$("#wu-datagrid").datagrid("reload");
								}
							}, "json");
					}

				});

	
}
     //打开添加框
	function openAdd(){
		    $("#add-wu-form").form("clear");
			$("#jhdivid").window("open"); //打开窗口
		  }
	
	//保存添加框
	function addSaveUser(){
    var t = $('#sectionId16').combotree('tree');	// get the tree object
    var n = t.tree('getSelected');		// get selected node
    alert(n.text);
	$.messager.confirm('提示', '确认添加吗?', function(r) {
					if(r) {
						$.post(
							globalData.server + "product/insertProduct?token="+globalData.getToken(), 	
							$("#add-wu-form").serialize()
							,
							function(res) {
								if(res>0){
									$.messager.alert("添加成功");
									return ;
								} else {
									$.messager.alert('提示',"添加成功");
									$("#add-wu-dialog").window("close");
									$("#wu-datagrid").datagrid("reload");
								}
							}, "json");
					}

				});
	
	}
	
	/*为用户添加角色*/
		function jueseAdd() {
			var row = $("#partRoles").datagrid("getRows");//返回当前页的行
			var selected = $("#AllRoles").datagrid("getSelected");
			//var roName = $("#AllRoles").datagrid("getSelected").rname
			if(selected != null) {
				for(var i = 0; i < row.length; i++) {
					if(row[i].rname == selected.rname) {
						$.messager.alert('提示', '用户已存在当前角色', 'error');
						return false;
					}
				}
				$.messager.confirm('提示', '确认添加吗?', function(r) {
					$.post(globalData.server + 'user/isUserAddJueSe', {
						uid: useridsss,
						rid: selected.rid,
						token: globalData.getToken()
					}, function(res) {
						$("#partRoles").datagrid("reload");
						window.top.loadTree();
					 }, "json");
				});
			} else {
				$.messager.alert('提示', '请先选中数据', 'error');
			}
		}
		/*为用户移除角色*/
		function jueseRemove() {
			var row = $("#partRoles").datagrid("getRows");
			if(row.length == 0) {
				$.messager.alert('提示', '你好    你已经没有角色可以移除', 'error');
				return false;
			} else {
				var selected = $("#partRoles").datagrid("getSelected");
				if(selected != null) {
					$.messager.confirm('提示', '确认移除吗?', function(r) {
						$.post(globalData.server + 'user/delUserRolesByUidAndRid', {
							uid: useridsss,
							rid: selected.rid,
							token: globalData.getToken()
						}, function(res) {
							$("#partRoles").datagrid("reload");
							window.top.loadTree();
						}, "json");
					})
				} else {
					$.messager.alert('提示', '请先选中数据', 'error');
				}
			}
		}
</script>